var productName = null;
var pnFlag = false;
var productUnit = null;
var puFlag = false;
var productCount = null;
var pcFlag = false;
var totalPrice = null;
var tpFlag = false;
var providerId = null;
var piFlag = false;
var addBtn = null;

function priceReg (value){
	value = value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
		value = value.replace(/^\./g,"");  //验证第一个字符是数字而不是.
    value = value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//去掉特殊符号￥
	if(value.indexOf(".")>0){
		value = value.substring(0,value.indexOf(".")+3);
	}
	return value;
}


$(function(){
	productName = $("#productName");
	productUnit = $("#productUnit");
	productCount = $("#productCount");
	totalPrice = $("#totalPrice");
	providerId = $("#providerId");
	addBtn = $("#save");

	productName.next().html("*");
	productUnit.next().html("*");
	productCount.next().html("*");
	totalPrice.next().html("*");
	providerId.next().html("*");
	
	$.ajax({
		type:"POST",
		url:"/bill/billAdd",
		data:{method:"getproviderlist"},
		dataType:"json",
		success:function(data){
			if(data != null){
				$("select").html("");//通过标签选择器，得到select标签，适用于页面里只有一个select
				var options = "<option value=\"0\">请选择</option>";
				for(var i = 0; i < data.length; i++){
					options += "<option value=\""+data[i].id+"\">"+data[i].proName+"</option>";
				}
				$("select").html(options);
			}
		},
		error:function(data){
			providerId.next().css({"color": "red"});
			providerId.next().html("* 获取供应商列表错误");
		}
	});

	
	productName.on("focus",function(){
		productName.next().css({"color": "#666666"});
		productName.next().html("* 请输入商品名称");
	}).on("blur",function(){
		if(productName.val() != null && productName.val() != ""){
			productName.next().css({"color": "green"});
			productName.next().html("* √");
			pnFlag = true;
		}else{
			productName.next().css({"color": "orange"});
			productName.next().html("* 商品名称不能为空，请重新输入");
		}
		
	});
	
	productUnit.on("focus",function(){
		productUnit.next().css({"color": "#666666"});
		productUnit.next().html("* 请输入商品单位");
	}).on("blur",function(){
		if(productUnit.val() != null && productUnit.val() !== ""){
			productUnit.next().css({"color": "green"});
			productUnit.next().html("* √");
			puFlag = true;
		}else{
			productUnit.next().css({"color": "orange"});
			productUnit.next().html("* 商品单位描述不能为空，请重新输入");
		}
		
	});
	
	providerId.on("focus",function(){
		providerId.next().css({"color": "#666666"});
		providerId.next().html("* 请选择供应商");
	}).on("blur",function(){
		if(providerId.val() != null && providerId.val() !== "" && providerId.val() !== 0){
			providerId.next().css({"color": "green"});
			providerId.next().html("* √");
			piFlag = true;
		}else{
			providerId.next().css({"color": "orange"});
			providerId.next().html("* 供应商不能为空，请选择");
		}
		
	});
	
	productCount.on("focus",function(){
		productCount.next().css({"color": "#666666"});
		productCount.next().html("* 请输入大于0的正自然数，小数点后保留2位");
	}).on("keyup",function(){
		this.value = priceReg(this.value);
	}).on("blur",function(){
		this.value = priceReg(this.value);
	});
	
	totalPrice.on("focus",function(){
		totalPrice.next().css({"color": "#666666"});
		totalPrice.next().html("* 请输入大于0的正自然数，小数点后保留2位");
	}).on("keyup",function(){
		this.value = priceReg(this.value);
	}).on("blur",function(){
		this.value = priceReg(this.value);
	});

	addBtn.on("click",function(){
		productName.blur();
		productUnit.blur();
		providerId.blur();

		if(pnFlag && puFlag && piFlag){
			if(confirm("是否确认提交数据")){
				$("#billForm").submit();
			}
		}else {
			swal("请填写正确信息！","","info");
		}
	});

});